package com.zx.controller;

import com.alibaba.fastjson.JSON;
import com.zx.entity.User;
import com.zx.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-11
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 查询用户
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public String findById(@PathVariable("id") Long id) {
        User user = userService.findById(id);
        return JSON.toJSONString(user);
    }


    /**
     * 添加用户
     *
     * @param user 用户实体
     * @return 用户
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public String addUser(@RequestBody User user) {
        User u = userService.addUser(user);
        return JSON.toJSONString(u);
    }

    /**
     * 修改用户
     *
     * @param user 用户实体
     * @return 用户
     */
    @PutMapping
    @ResponseStatus(HttpStatus.CREATED)
    public String updateUser(@RequestBody User user) {
        User u = userService.updateUser(user);
        return JSON.toJSONString(u);
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     */
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public String deleteProduct(@PathVariable("id") Long id) {
        userService.deleteById(id);
        return "删除用户成功";
    }

}
